package com.kyzh.core.pager.gamedetail

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gushenge.atools.ui.arcButton
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setHeight
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.beans.Server
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.GameDetailServerAdapter
import com.kyzh.core.uis.recyclerView
import com.kyzh.core.uis.smartRefreshLayout
import com.kyzh.core.utils.ViewUtils.dp
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider
import org.jetbrains.anko.*
import org.jetbrains.anko.support.v4.UI

class GameServerFragment: BaseFragment(),ResultListener {

    lateinit var recycler: RecyclerView
    lateinit var root: LinearLayout
    lateinit var refresh: SmartRefreshLayout
    lateinit var adapter:GameDetailServerAdapter
    val servers = arrayListOf<Server>()
    var p=1
    var max=1
    lateinit var context:Activity
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        context = requireActivity()
        return UI {
            root = verticalLayout {
                backgroundColor = Color.WHITE
                arcButton(getString(R.string.referenceOnly)){
                    textColor = resources.getColor(R.color.font_88)
                    textSize = 13f
                    gravity = Gravity.CENTER_VERTICAL
                    setBackgroundColor(ContextCompat.getColor(requireActivity(),R.color.bg_f5))
                    gravity = Gravity.CENTER
                } .lparams(dip(279), dip(18)){
                    gravity = Gravity.CENTER_HORIZONTAL
                    topMargin = dip(10)
                    bottomMargin = dip(10)
                }

                refresh = smartRefreshLayout {
                    setEnableRefresh(true)
                    setEnableAutoLoadMore(false)
                    setEnableOverScrollBounce(true)
                    setEnableLoadMoreWhenContentNotFull(false)
                    setEnableNestedScroll(true)

                    setOnLoadMoreListener {
                        if (p>max){
                            toast(getString(R.string.noHaveMore))
                            finishLoadMore()
                            setEnableLoadMore(false)

                        }else{
                            setEnableLoadMore(true)
                            GameImpl.gameDetailServer(p,this@GameServerFragment)
                        }
                    }
                    setOnRefreshListener {
                        p=1
                        servers.clear()
                        GameImpl.gameDetailServer(1,this@GameServerFragment)
                    }
                    recycler = recyclerView{
                        layoutManager = object :LinearLayoutManager(context){
                            override fun canScrollHorizontally(): Boolean {
                                return false
                            }
                        }
                    }.lparams(matchParent, matchParent)
                }.lparams(matchParent, matchParent)
            }

        }.view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        super.onViewCreated(view, savedInstanceState)

        AView.setWidth(root, matchParent)
        AView.setHeight(root, matchParent)
        adapter = GameDetailServerAdapter(R.layout.game_detail_server_item,servers)
        recycler.adapter = adapter
        refresh.setEnableFooterFollowWhenNoMoreData(true)
        refresh.setScrollBoundaryDecider(object : ScrollBoundaryDecider {
            override fun canRefresh(content: View?): Boolean {
                return recycler.computeVerticalScrollOffset()==0
            }

            override fun canLoadMore(content: View?): Boolean {
                return true
            }
        })
        GameImpl.gameDetailServer(1,this)
    }

    override fun success(beans: Any, p: Int, max: Int) {
        this.p = p
        this.max = max
        if (p==2){
            servers.clear()
        }
        servers.addAll(beans as ArrayList<Server>)
        adapter.notifyDataSetChanged()
        refresh.setEnableLoadMore(true)
        refresh.finishRefresh()
        refresh.finishLoadMore()
    }

    override fun error() {
        val inflate = LayoutInflater.from(requireContext()).inflate(R.layout.empty, null)
        inflate.post {
            inflate.setHeight(200.dp)
            val tv1 = inflate.findViewById<TextView>(R.id.tv1)
            tv1.text = "动态开服"

        }
        adapter.setEmptyView(inflate)
        refresh.setEnableLoadMore(false)
        refresh.finishRefresh()
        refresh.finishLoadMore()
    }
}